package listener.physical;

import java.util.Observable;
import java.util.Observer;

import model.GlobalParameters;
import model.Particle;

public class WallCollision implements Observer{
	private static WallCollision wallCollision;
	
	public static WallCollision getInstance(){
		if(wallCollision == null){
			wallCollision = new WallCollision();
		}
		return wallCollision;
	}
	
	public void collision(Particle p){
		//撞墙
		double x = p.getPosition().getX();
		double y = p.getPosition().getY();
		if(x < 0 || x+p.getRadius() > GlobalParameters.Width){
			p.getVelocity().setX(-p.getVelocity().getX());
			if( x < 0){
				p.getPosition().setX(1);
			}else{
				p.getPosition().setX(GlobalParameters.Width-p.getRadius());
			}
		}
		if(y<0 || y+p.getRadius() > GlobalParameters.Height){
			p.getVelocity().setY(-p.getVelocity().getY());
			if( y < 0){
				p.getPosition().setY(1);
			}else{
				p.getPosition().setY(GlobalParameters.Height-p.getRadius());
			}
		}
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		Particle p = (Particle)arg0;
		String order = (String) arg1;
		
		if(order.equals("move")){
			collision(p);
		}
	}

}
